'Option Explicit

Dim doc
Set doc = application.ActiveDocument
main

' Function to validate document
Private Function ValidateServer(docObj)
    Dim key, licenseServer, licenseToken
    key = docObj.Validate(0)
    Set licenseServer = CreateObject("MGCPCBAutomationLicensing.Application")
    licenseToken = licenseServer.GetToken(key)
    Set licenseServer = nothing
    'On Error Resume Next
    Err.Clear
    docObj.Validate(licenseToken)
    If Err Then
        ValidateServer = 0    
    Else 
        ValidateServer = 1
    End If
End Function

sub GetUserLicence(docObj)

	If ValidateServer(docObj) = 0 Then
	    msgbox "Server validation failed."
	    exit sub
	End If
	
end sub
'---------------------------------------------------------------------

sub main()
	GetUserLicence(doc)
	TraceVoid 
	
end sub

sub TraceVoid()

	Dim mskeng
	Set mskeng = CreateObject("MGCPCBEngines.MaskEngine")
	mskeng.CurrentUnit = 5
	doc.CurrentUnit=5
	booleanOpOr =2
	application.LockServer
	Dim lyr: lyr = 1
	Dim nlyr:nlyr = doc.LayerCount
	


	Dim shapesTrace
	Set maskTrace = mskeng.Masks.Add
    Set shapesTrace = maskTrace.Shapes
    Set clgs = doc.ConductorLayerGfxs(epcbCondGfxTrace, epcbSelectSelected, application.Gui.ActiveRouteLayer, False)

	Dim usrVoidWidth
	'usrVoidWidth = inputbox("Please input Void width:",usrVoidWidth,350)
	usrVoidWidth = 150

    For Each clg In clgs
        pnts = clg.Geometry.PointsArray
        lw = clg.Geometry.LineWidth + (usrVoidWidth * 2)      
        Call shapesTrace.AddByPath(1 + UBound(pnts, 2), pnts, lw, True, emeOversizeTypeRound) ' make round ends
    Next

    Set maskTrace = mskeng.BooleanOp(booleanOpOr, maskTrace, maskTrace)
    
    For Each shp In maskTrace.Shapes
        pnts = shp.PointsArray

        If shp.Solid Then

        	if application.Gui.ActiveRouteLayer=1 then
            	Set ukg = doc.PutObstruct(application.Gui.ActiveRouteLayer, 10, 1 + UBound(pnts, 2), pnts, epcbObstructPlane, False, Nothing, epcbUnitUM)
            	Set ukg = doc.PutObstruct(2, 10, 1 + UBound(pnts, 2), pnts, epcbObstructPlane, False, Nothing, epcbUnitUM)
           		 'Call ukg.putproperty("SENSE_LINE_VOID","SENSE_LINE_VOID")
           	end if
           	
			if application.Gui.ActiveRouteLayer=nlyr then
            	Set ukg = doc.PutObstruct(nlyr, 10, 1 + UBound(pnts, 2), pnts, epcbObstructPlane, False, Nothing, epcbUnitUM)
            	Set ukg = doc.PutObstruct(nlyr-1, 10, 1 + UBound(pnts, 2), pnts, epcbObstructPlane, False, Nothing, epcbUnitUM)
           	end if
           	
           	if application.Gui.ActiveRouteLayer<>1 and application.Gui.ActiveRouteLayer<>nlyr then
            	Set ukg = doc.PutObstruct(application.Gui.ActiveRouteLayer, 10, 1 + UBound(pnts, 2), pnts, epcbObstructPlane, False, Nothing, epcbUnitUM)
           	end if
           	
        End If
        

    Next
	
	
end sub


